package com.lin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lin.entity.GroupInformation;
import com.lin.entity.TalentJoin;
import com.lin.entity.Talents;
import com.lin.mapper.GroupInformationMapper;
import com.lin.mapper.TalentJoinMapper;
import com.lin.security.util.SecurityUtil;
import com.lin.service.ProjectDetailService;
import com.lin.service.ProjectOutsideService;
import com.lin.service.TalentJoinService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.service.TalentsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 林伟鸿
 * @since 2022-04-16
 */
@Service
@SuppressWarnings({"all"})
public class TalentJoinServiceImpl extends ServiceImpl<TalentJoinMapper, TalentJoin> implements TalentJoinService {
    @Resource
    TalentsService talentsService;

    @Resource
    ProjectOutsideService projectOutsideService;

    @Resource
    ProjectDetailService projectDetailService;

    @Resource
    GroupInformationMapper groupInformationMapper;


    @Override
    public void joinProject(TalentJoin talentJoin) {
        int currentUserUid = SecurityUtil.getCurrentUserUid();
        if (projectOutsideService.getById(talentJoin.getProjectId()) != null) { //判断项目是否存在

            Integer groupId = projectDetailService.getById(talentJoin.getProjectId()).getGroupId();
            GroupInformation groupInformation = groupInformationMapper.selectOne(new QueryWrapper<GroupInformation>()
                    .eq("group_id", groupId));
            //判断是否已经在队伍当中
            Integer masterId = groupInformation.getMasterId();
            Integer memberOneId = groupInformation.getMemberOneId();
            Integer memberTwoId = groupInformation.getMemberTwoId();
            Integer memberThreeId = groupInformation.getMemberThreeId();
            Integer memberFourId = groupInformation.getMemberFourId();
            if ((masterId.equals(currentUserUid)) || (memberOneId != null && memberOneId.equals(currentUserUid))
                    || (memberTwoId != null && memberTwoId.equals(currentUserUid))
                    || (memberThreeId != null && memberThreeId.equals(currentUserUid))
                    || (memberFourId != null) && memberFourId.equals(currentUserUid)) {
                throw new RuntimeException("您已经是团队成员！");
            } else {
                if (this.getOne(new QueryWrapper<TalentJoin>()
                        .eq("project_id", talentJoin.getProjectId())
                        .eq("user_id", currentUserUid)) == null) {//判断是否重复申请
                    Talents talents = talentsService.getById(currentUserUid);
                    talentJoin.setUserId(talents.getUid());
                    talentJoin.setTalentName(talents.getTalentName());
                    talentJoin.setTalentTag(talents.getTalentTag());
                    this.saveOrUpdate(talentJoin);
                } else {
                    throw new RuntimeException("30天内,请不要重复申请！");
                }
            }
        } else {
            throw new RuntimeException("此项目不存在！");
        }

    }

    @Override
    public void joinProcess(TalentJoin talentJoin) {
        TalentJoin talentJoinDB = this.getById(talentJoin);
        talentJoinDB.setType(talentJoin.getType());
        this.updateById(talentJoinDB);
        if (talentJoinDB.getType() == 0) {//同意入队
            Integer groupId = projectDetailService.getById(talentJoinDB.getProjectId()).getGroupId();
            GroupInformation groupInformation = groupInformationMapper.selectOne(new QueryWrapper<GroupInformation>()
                    .eq("group_id", groupId));
            Integer masterId = groupInformation.getMasterId();
            Integer memberOneId = groupInformation.getMemberOneId();
            Integer memberTwoId = groupInformation.getMemberTwoId();
            Integer memberThreeId = groupInformation.getMemberThreeId();
            Integer memberFourId = groupInformation.getMemberFourId();
            Integer userId = talentJoinDB.getUserId();
            if ((masterId.equals(userId)) || (memberOneId != null && memberOneId.equals(userId))
                    || (memberTwoId != null && memberTwoId.equals(userId))
                    || (memberThreeId != null && memberThreeId.equals(userId))
                    || (memberFourId != null) && memberFourId.equals(userId)) {
                throw new RuntimeException("该成员已经在团队当中！");
            } else {
                if (memberOneId == null) {
                    groupInformation.setMemberOneId(talentJoinDB.getUserId());
                }else if (memberTwoId == null) {
                    groupInformation.setMemberTwoId(talentJoinDB.getUserId());
                }else if (memberThreeId == null) {
                    groupInformation.setMemberThreeId(talentJoinDB.getUserId());
                }else if (memberFourId == null) {
                    groupInformation.setMemberFourId(talentJoinDB.getUserId());
                }else {
                    throw new RuntimeException("团队已经满员！");
                }
                groupInformationMapper.updateById(groupInformation);
            }
        }
    }
}
